<template>
	<view>
		<movable-area class="movable-area" :scale-area="false">
			<movable-view class="movable-view" :class="!isRemove?'animation-info':''" style="pointer-events: auto;"
				@click="clickBtn" @touchstart="touchstart" @touchend="touchend" @change="onChange" direction="all"
				inertia="true" :x="x" :y="y" :disabled="disabled" :out-of-bounds="true" :damping="200" :friction="100">
				<slot>
					<image src="/static/mine/online.png" @click="goChat" style="width: 86rpx;height: 88rpx;"></image>
					<image src="/static/mine/phone.png" @click="open"
						style="width: 86rpx;height: 88rpx;margin-top: 12rpx;"></image>
				</slot>
			</movable-view>
		</movable-area>
		<u-popup :show="show" @close="close" mode="center" bgColor="transparent">
			<view class="centerPopup">
				<image src="/static/mine/pjonePopup.png" mode=""></image>
				<view class="serviceList">
					<view class="services-item" v-for="(item,index) in list" :key="index">
						<view class="item">
							<view class="img">
								<image :src="item.image" mode=""></image>
								<view class="info">
									<view class="name">{{item.name}}</view>
									<view class="phone">{{item.phone}}</view>
								</view>
							</view>
							<view class="make" @click="makePhone">拨打电话</view>
						</view>
					</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		props: {
			//是否禁用拖动
			disabled: {
				type: Boolean,
				default: false
			},
			//是否自动停靠
			canDocking: {
				type: Boolean,
				default: true
			},
			//按钮默认位置离底部距离（px）
			bottomPx: {
				type: Number,
				default: 30
			},
			//按钮默认位置离右边距离（px）
			rightPx: {
				type: Number,
				default: 0
			},
			userInfo: {
				type: Object,
			}
		},
		data() {
			return {
				left: 0,
				top: 0,
				isRemove: true,
				windowWidth: 0,
				windowHeight: 0,
				btnWidth: 0,
				btnHeight: 0,
				x: 10000,
				y: 10000,
				old: {
					x: 0,
					y: 0
				},
				show: false,
				list: [{
					name: '客服一号',
					phone: '4008007526',
					image: '/static/mine/serviceImg.png'
				}]
			};
		},
		mounted() {
			this.getSysInfo()
			console.log(this.userInfo);
		},
		methods: {
			getSysInfo() {
				let sysInfo = uni.getSystemInfoSync()
				this.windowWidth = sysInfo.windowWidth
				this.windowHeight = sysInfo.windowHeight
				let view = uni.createSelectorQuery().in(this).select(".movable-view")
				view.boundingClientRect(rect => {
					this.btnWidth = rect.width
					this.btnHeight = rect.height
					this.x = this.old.x
					this.y = this.old.y
					this.$nextTick(res => {
						this.x = this.windowWidth - this.btnWidth - this.rightPx
						this.y = this.windowHeight - this.btnHeight - this.bottomPx
					})
				}).exec()
			},
			//移动按钮
			onChange(e) {
				this.old.x = e.detail.x
				this.old.y = e.detail.y
			},
			//开始移动
			touchstart(e) {
				this.isRemove = true
			},
			//结束移动
			touchend(e) {
				if (this.canDocking && this.old.x) {
					this.x = this.old.x
					this.y = this.old.y
					let bWidth = (this.windowWidth - this.btnWidth) / 2
					if (this.x < 0 || (this.x > 0 && this.x <= bWidth)) {
						this.$nextTick(res => {
							this.x = 0
						})
					} else {
						this.$nextTick(res => {
							this.x = this.windowWidth - this.btnWidth
						})
					}
					this.isRemove = false
				}
			},
			//点击按钮
			clickBtn() {
				this.$emit('clickBtn')
			},
			open() {
				setTimeout(() => {
					this.$refs.popup.open('center')
				}, 100)
			},
			open(){
				this.show = true
			},
			close(){
				this.show = false
			},
			goChat() {
				if (this.userInfo.UserId == '99408893') {
					uni.navigateTo({
						url: `/pages/subpage/customer/conversations?id=${this.userInfo.UserId}&avatar=${this.userInfo.avatar}&name=${this.userInfo.userName+":"+this.userInfo.phone}`
					})
				} else {
					// 带自己的信息
					let params = {
						data: {
							avatar: this.userInfo.avatar,
							name: this.userInfo.userName + ":" + this.userInfo.phone,
						},
						lastMessage: 'at',
						top: false,
						type: "private",
						unread: 0,
						userId: this.userInfo.UserId
					}
					uni.navigateTo({
						url: `/pages/subpage/customer/privateChat?to=${JSON.stringify(params)}&id=${this.userInfo.UserId}`
					})
				}

			},
			makePhone() {
				//#ifdef H5
				window.location.href = 'tel:' + '4008007526'
				//#endif
				//#ifdef APP-PLUS
				plus.device.dial('4008007526', true);
				//#endif
			}
		}
	};
</script>

<style scoped lang="scss">
	.movable-view {
		width: 110rpx;
		height: 288rpx;
		background-image: url(/static/mine/services.png);
		background-repeat: no-repeat;
		background-size: contain;
		color: #FFFFFF;
		font-size: 26rpx;
		touch-action: none;
		display: flex;
		flex-direction: column;
		align-items: center;
		padding-top: 88rpx;
	}

	.animation-info {
		transition: left .25s ease;
	}

	.movable-area {
		width: 100%;
		height: 100%;
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		z-index: 100 !important;
		pointer-events: none;
	}

	.centerPopup {
		image {
			width: 510rpx;
			height: 184rpx;
		}

		.serviceList {
			background-color: #fff;
			margin-top: -40rpx;
			border-radius: 30rpx;
			padding: 28rpx;
			position: relative;
			z-index: 100;
			height: 284rpx;
			.services-item {
				.item {
					display: flex;
					align-items: center;
					justify-content: space-between;
					.img{
						display: flex;
						align-items: center;
						image{
							width: 60rpx;
							height: 60rpx;
						}
						.info{
							margin-left: 13rpx;
							.name{
								font-weight: 400;
								font-size: 26rpx;
								color: #333333;
							}
							.phone{
								font-weight: 400;
								font-size: 22rpx;
								color: #333333;
								margin-top: 10rpx;
							}
						}
					}
					.make{
						width: 136rpx;
						background: linear-gradient( 137deg, #3D9BFF 0%, #2E92FE 100%);
						border-radius: 24rpx;
						text-align: center;
						line-height: 48rpx;
						font-weight: 400;
						font-size: 26rpx;
						color: #FFFFFF;
					}
				}
			}
		}

	}
</style>